package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.DeviceDao;
import com.wanlian.dao.DeviceDataDao;
import com.wanlian.entity.Device;
import com.wanlian.entity.DeviceData;
import com.wanlian.model.ResultModel;
import com.wanlian.service.DeviceDataServices;
import com.wanlian.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class DeviceDataServicesImpl implements DeviceDataServices {


    @Autowired
    public DeviceDataDao deviceDataDao;

    @Autowired
    public DeviceDao deviceDao;

    @Override
    public ResultModel doAdd(DeviceData deviceData) {
        ResultModel resultModel = new ResultModel();
        deviceData.setModifyTime(DateUtil.currentDateTimestamp());
        Device device = deviceDao.queryByDeviceKey(deviceData.getDeviceKey());
        if (device != null) {
            DeviceData deviceDatadb = deviceDataDao.queryByDate(deviceData.getDeviceKey(), DateUtil.currentDateTimestamp());
            if (deviceDatadb == null) {
                deviceDataDao.add(deviceData);
            } else {
                deviceDatadb.setMaxBw(deviceData.getMaxBw());
                int daySecond = 3600 * 24;
                float onlineRate = (float) deviceData.getOnlineTime() / daySecond;
                if (onlineRate > 1) {
                    onlineRate = 1.0f;
                }
                deviceDatadb.setOnlineLv(onlineRate);
                deviceDatadb.setOnlineTime(deviceData.getOnlineTime());
                deviceDataDao.update(deviceDatadb);
            }
        }
        resultModel.setMessage("添加成功");
        return resultModel;
    }

    @Override
    public ResultModel doAdd(List<DeviceData> deviceDatas) {
        Map<String, DeviceData> deviceDataMap = deviceDataDao.queryAllData(DateUtil.currentDateTimestamp());
        Map<String, Device> deviceMap = deviceDao.queryAllDevices();
        for (DeviceData item : deviceDatas) {
            if (deviceMap.containsKey(item.getDeviceKey())) {
                if (deviceDataMap.containsKey(item.getDeviceKey())) {
                    DeviceData deviceData = deviceDataMap.get(item.getDeviceKey());
                    deviceData.setMaxBw(item.getMaxBw());
                    int daySecond = 3600 * 24;
                    float onlineRate = (float) item.getOnlineTime() / daySecond;
                    if (onlineRate > 1) {
                        onlineRate = 1.0f;
                    }
                    deviceData.setOnlineLv(onlineRate);
                    deviceData.setOnlineTime(item.getOnlineTime());
                    deviceDataDao.update(deviceData);
                } else {
                    deviceDataDao.add(item);
                }
            }
        }
        return new ResultModel(true, null, "添加或修改成功");
    }

    @Override
    public List<DeviceData> editBatch(List<DeviceData> deviceDatas) {
        Map<String, DeviceData> deviceDataMap = deviceDataDao.queryAllData(DateUtil.currentDateTimestamp());
        List<String> devicelist = deviceDao.queryAllDeviceList();
        List<DeviceData> deviceDataList_UPDATE = new ArrayList<>();
        List<DeviceData> deviceDataList_INSERT = new ArrayList<>();
        for (DeviceData item : deviceDatas) {
            if (devicelist.contains(item.getDeviceKey())) {
                if (deviceDataMap.containsKey(item.getDeviceKey())) {
                    DeviceData deviceData = deviceDataMap.get(item.getDeviceKey());
                    deviceData.setMaxBw(item.getMaxBw());
                    int daySecond = 3600 * 24;
                    float onlineRate = (float) item.getOnlineTime() / daySecond;
                    if (onlineRate > 1) {
                        onlineRate = 1.0f;
                    }
                    deviceData.setOnlineLv(onlineRate);
                    deviceData.setOnlineTime(item.getOnlineTime());
                    deviceDataList_UPDATE.add(deviceData);

                } else {
                    int daySecond = 3600 * 24;
                    float onlineRate = (float) item.getOnlineTime() / daySecond;
                    if (onlineRate > 1) {
                        onlineRate = 1.0f;
                    }
                    item.setOnlineLv(onlineRate);
                    item.setModifyTime(DateUtil.currentDateTimestamp());
                   // deviceDataDao.insert(item);
                    deviceDataList_INSERT.add(item);
                }
            }
        }
        if (deviceDataList_UPDATE.size() > 0) {
            deviceDataDao.editBatch(deviceDataList_UPDATE);
        }
        return deviceDataList_INSERT;
    }


    @Override
    public void doInsertsBatch( List<DeviceData> deviceDataList){
        deviceDataDao.doInsertBatch(deviceDataList);
    }

    @Override
    public ResultModel queryByParam(PageCondition pageCondition) {
        return deviceDataDao.queryDeviceDataByParams(pageCondition);
    }

    /**
     * 获取所有设备当天在线时长
     *
     * @param date
     * @return
     */
    @Override
    public List<DeviceData> queryAllData(String date) {
        return deviceDataDao.queryAllData(date);
    }

    public static void main(String[] args) {
        int daysencond = 3600 * 24;
        float onlineRate = (float) 10000 / 86400;
        System.out.println(onlineRate);
    }
}
